home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 123 / MacAddict_123_2006_11.iso / Software / Utilities / Local Weather 1.1.0.9.wdgt / AppleClasses / AppleScrollArea.js < prev    next >
Text File  |  2005-12-12  |  9KB  |  341 lines

  1. /*
  2. ¬© Copyright 2005 Apple Computer, Inc. All rights reserved.
  3.  
  4. IMPORTANT:  This Apple software and the associated images located in
  5. /System/Library/WidgetResources/AppleClasses/ (collectively "Apple Software")
  6. are supplied to you by Apple Computer, Inc. (‚ÄúApple‚Äù) in consideration of your
  7. agreement to the following terms. Your use, installation and/or redistribution
  8. of this Apple Software constitutes acceptance of these terms. If you do not
  9. agree with these terms, please do not use, install, or redistribute this Apple
  10. Software.
  11.  
  12. In consideration of your agreement to abide by the following terms, and subject
  13. to these terms, Apple grants you a personal, non-exclusive license, under
  14. Apple‚Äôs copyrights in the Apple Software, to use, reproduce, and redistribute
  15. the Apple Software, in text form (for JavaScript files) or binary form (for
  16. associated images), for the sole purpose of creating Dashboard widgets for Mac
  17. OS X.
  18.  
  19. If you redistribute the Apple Software, you must retain this entire notice and
  20. the warranty disclaimers and limitation of liability provisions (last two
  21. paragraphs below) in all such redistributions of the Apple Software.
  22.  
  23. You may not use the name, trademarks, service marks or logos of Apple to endorse
  24. or promote products that include the Apple Software without the prior written
  25. permission of Apple. Except as expressly stated in this notice, no other rights
  26. or licenses, express or implied, are granted by Apple herein, including but not
  27. limited to any patent rights that may be infringed by your products that
  28. incorporate the Apple Software or by other works in which the Apple Software may
  29. be incorporated.
  30.  
  31. The Apple Software is provided on an "AS IS" basis.  APPLE MAKES NO WARRANTIES,
  32. EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
  33. NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
  34. REGARDING THE APPPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION
  35. WITH YOUR PRODUCTS.
  36.  
  37. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR DISTRIBUTION OF THE
  41. APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42. (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44.  */
  45.  
  46. /*
  47.  * AppleScrollArea Constructor
  48.  * content is the element containing the display to be scrolled.
  49.  * Any additional arguments will be added as scrollbars using this.addScrollbar.
  50.  */
  51. function AppleScrollArea(content)
  52. {
  53.     /* Objects */
  54.     this.content = content;
  55.     
  56.     /* public properties */
  57.     // These are read-write. Set them as needed.
  58.     this.scrollsVertically = true;
  59.     this.scrollsHorizontally = true;
  60.     this.singlepressScrollPixels = 10;
  61.     
  62.     // These are read-only.
  63.     this.viewHeight = 0;
  64.     this.viewToContentHeightRatio = 1.0;
  65.     this.viewWidth = 0;
  66.     this.viewToContentWidthRatio = 1.0;
  67.  
  68.     /* Internal Objects */
  69.     this._scrollbars = new Array();
  70.     
  71.     // For JavaScript event handlers
  72.     var _self = this;
  73.     
  74.     /*
  75.      * Privileged methods
  76.      * These event handlers need to be here because within an event handler,
  77.      * "this" refers to the element which called the event, rather than the 
  78.      * class instance.
  79.      */    
  80.     this._refreshHandler = function() { _self.refresh(); };
  81.     this._keyPressedHandler = function() { _self.keyPressed(event); };
  82.     this._mousewheelScrollHandler = function(event) { _self.mousewheelScroll(event); };
  83.  
  84.     // Set up the style for the content element just to be certain
  85.     this.content.style.overflow = "hidden";
  86.     this.content.scrollTop = 0;
  87.     this.content.scrollLeft = 0;
  88.     
  89.     // Add event listeners
  90.     this.content.addEventListener("mousewheel", this._mousewheelScrollHandler, true);
  91.     
  92.     this.refresh();
  93.     
  94.     // Add any scrollbars
  95.     var c = arguments.length;
  96.     for (var i = 1; i < c; ++i)
  97.     {
  98.         this.addScrollbar(arguments[i]);
  99.     }
  100. }
  101.  
  102. AppleScrollArea.prototype.addScrollbar = function(scrollbar)
  103. {
  104.     scrollbar.setScrollArea(this);
  105.     this._scrollbars.push(scrollbar);
  106.     scrollbar.refresh();
  107. }
  108.  
  109. AppleScrollArea.prototype.removeScrollbar = function(scrollbar)
  110. {
  111.     var scrollbars = this._scrollbars;
  112.     var c = scrollbars.length;
  113.     for (var i = 0; i < c; ++i)
  114.     {
  115.         if (scrollbars[i] == scrollbar)
  116.         {
  117.             scrollbars.splice(i, 1);
  118.             break;
  119.         }
  120.     }
  121. }
  122.  
  123. AppleScrollArea.prototype.remove = function()
  124. {
  125.     this.content.removeEventListener("mousewheel", this._mousewheelScrollHandler, true);
  126.     
  127.     var scrollbars = this._scrollbars;
  128.     var c = scrollbars.length;
  129.     for (var i = 0; i < c; ++i)
  130.     {
  131.         scrollbars[i].setScrollArea(null);
  132.     }
  133. }
  134.  
  135. /*
  136.  * refresh() member function
  137.  * Refresh the current scrollbar position and size.
  138.  * This should be called whenever the content element changes.
  139.  */
  140. AppleScrollArea.prototype.refresh = function()
  141. {    
  142.     // get the current actual view height. Float because we divide.
  143.     var style = document.defaultView.getComputedStyle(this.content, '');
  144.     if (style)
  145.     {
  146.        this.viewHeight = parseFloat(style.getPropertyValue("height"));
  147.        this.viewWidth  = parseFloat(style.getPropertyValue("width"));
  148.     }
  149.     else
  150.     {
  151.         this.viewHeight = 0;
  152.         this.viewWidth = 0;
  153.     }
  154.        
  155.     
  156.     if (this.content.scrollHeight > this.viewHeight)
  157.         this.viewToContentHeightRatio = this.viewHeight / this.content.scrollHeight;
  158.     else
  159.     {
  160.         this.viewToContentHeightRatio = 1.0;
  161.         this.verticalScrollTo(0);
  162.     }
  163.     
  164.     if (this.content.scrollWidth > this.viewWidth)
  165.         this.viewToContentWidthRatio = this.viewWidth / this.content.scrollWidth;
  166.     else
  167.     {
  168.         this.viewToContentWidthRatio = 1.0;
  169.         this.horizontalScrollTo(0);
  170.     }
  171.     
  172.     var scrollbars = this._scrollbars;
  173.     var c = scrollbars.length;
  174.     for (var i = 0; i < c; ++i)
  175.     {
  176.         scrollbars[i].refresh();
  177.     }
  178. }
  179.  
  180. /*
  181.  * focus() member function.
  182.  * Tell the scrollarea that it is in focus. It will capture keyPressed events
  183.  * and if they are arrow keys scroll accordingly.
  184.  */
  185. AppleScrollArea.prototype.focus = function()
  186. {
  187.     document.addEventListener("keypress", this._keyPressedHandler, true);
  188. }
  189.  
  190. /*
  191.  * blur() member function.
  192.  * Tell the scrollarea that it is no longer in focus. It will cease capturing
  193.  * keypress events.
  194.  */
  195. AppleScrollArea.prototype.blur = function()
  196. {
  197.     document.removeEventListener("keypress", this._keyPressedHandler, true);
  198. }
  199.  
  200.  
  201. /*
  202.  * reveal(element) member function.
  203.  * Pass in an Element which is contained within the content element.
  204.  * The content will then be scrolled to reveal that element.
  205.  */
  206. AppleScrollArea.prototype.reveal = function(element)
  207. {
  208.     var offsetY = 0;
  209.     var obj = element;
  210.     do
  211.     {
  212.         offsetY += obj.offsetTop;
  213.         obj = obj.offsetParent;
  214.     } while (obj && obj != this.content);
  215.     
  216.     var offsetX = 0;
  217.     obj = element;
  218.     do
  219.     {
  220.         offsetX += obj.offsetLeft;
  221.         obj = obj.offsetParent;
  222.     } while (obj && obj != this.content);
  223.     
  224.     this.verticalScrollTo(offsetY);
  225.     this.horizontalScrollTo(offsetX);
  226. }
  227.  
  228.  
  229. AppleScrollArea.prototype.verticalScrollTo = function(new_content_top)
  230. {
  231.     if (!this.scrollsVertically)
  232.         return;
  233.     
  234.     var bottom = this.content.scrollHeight - this.viewHeight;
  235.     
  236.     if (new_content_top < 0)
  237.     {
  238.         new_content_top = 0;
  239.     }
  240.     else if (new_content_top > bottom)
  241.     {
  242.         new_content_top = bottom;
  243.     }
  244.     
  245.     this.content.scrollTop = new_content_top;
  246.     
  247.     var scrollbars = this._scrollbars;
  248.     var c = scrollbars.length;
  249.     for (var i = 0; i < c; ++i)
  250.     {
  251.         scrollbars[i].verticalHasScrolled();
  252.     }
  253. }
  254.  
  255. AppleScrollArea.prototype.horizontalScrollTo = function(new_content_left)
  256. {
  257.     if (!this.scrollsHorizontally)
  258.         return;
  259.     
  260.     var right = this.content_width - this.viewWidth;
  261.     
  262.     if (new_content_left < 0)
  263.     {
  264.         new_content_left = 0;
  265.     }
  266.     else if (new_content_left > right)
  267.     {
  268.         new_content_left = right;
  269.     }
  270.     
  271.     this.content.scrollLeft = new_content_left;
  272.     
  273.     var scrollbars = this._scrollbars;
  274.     var c = scrollbars.length;
  275.     for (var i = 0; i < c; ++i)
  276.     {
  277.         scrollbars[i].horizontalHasScrolled();
  278.     }
  279. }
  280.  
  281. /*********************
  282.  * Keypressed events
  283.  */
  284. AppleScrollArea.prototype.keyPressed = function(event)
  285. {
  286.     var handled = true;
  287.     
  288.     if (event.altKey)
  289.         return;
  290.     if (event.shiftKey)
  291.         return;
  292.     
  293.     switch (event.keyIdentifier)
  294.     {
  295.         case "Home":
  296.             this.verticalScrollTo(0);
  297.             break;
  298.         case "End":
  299.             this.verticalScrollTo(this.content.scrollHeight - this.viewHeight);
  300.             break;
  301.         case "Up":
  302.             this.verticalScrollTo(this.content.scrollTop - this.singlepressScrollPixels);
  303.             break;
  304.         case "Down":
  305.             this.verticalScrollTo(this.content.scrollTop + this.singlepressScrollPixels);
  306.             break;
  307.         case "PageUp":
  308.             this.verticalScrollTo(this.content.scrollTop - this.viewHeight);
  309.             break;
  310.         case "PageDown":
  311.             this.verticalScrollTo(this.content.scrollTop + this.viewHeight);
  312.             break;
  313.         case "Left":
  314.             this.horizontalScrollTo(this.content.scrollLeft - this.singlepressScrollPixels);
  315.             break;
  316.         case "Right":
  317.             this.horizontalScrollTo(this.content.scrollLeft + this.singlepressScrollPixels);
  318.             break;
  319.         default:
  320.             handled = false;
  321.     }
  322.     
  323.     if (handled)
  324.     {
  325.         event.stopPropagation();
  326.         event.preventDefault();
  327.     }
  328. }
  329.  
  330. /*********************
  331.  * Scrollwheel events
  332.  */
  333. AppleScrollArea.prototype.mousewheelScroll = function(event)
  334. {
  335.     var deltaScroll = event.wheelDelta / 120 * this.singlepressScrollPixels;
  336.     this.verticalScrollTo(this.content.scrollTop - deltaScroll);
  337.  
  338.     event.stopPropagation();
  339.     event.preventDefault();
  340. }
  341.